package com.centaline.androidsalesblog.rx.request;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.AppUpdateApi;
import com.centaline.androidsalesblog.model.businissobject.AppUpdateBo;
import com.centaline.androidsalesblog.model.responseobject.AppUpdateRo;
import com.centanet.framework.interfaces.IResponseListener;

import rx.Observable;
import rx.Subscriber;

/**
 * app 升级
 * Created by guilin on 16/5/3.
 */
final class RequestAppUpdateOnSubscribe implements Observable.OnSubscribe<AppUpdateBo> {

    private final Class<?> tag;

    public RequestAppUpdateOnSubscribe(Class<?> tag) {
        this.tag = tag;
    }

    @Override
    public void call(final Subscriber<? super AppUpdateBo> subscriber) {
        AppUpdateApi appUpdateApi = new AppUpdateApi(tag, AppUpdateRo.class,
                new IResponseListener<AppUpdateRo>() {
                    @Override
                    public void onResponse(AppUpdateRo appUpdateRo) {
                        if (!subscriber.isUnsubscribed()) {
                            subscriber.onNext(appUpdateRo.getAppUpdateBo());
                        }
                        subscriber.onCompleted();
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        subscriber.onError(error);
                    }
                });
        appUpdateApi.startRequest();
    }
}
